package com.framework.web.sys.controller;


import com.framework.core.annotation.SystemControllerLog;
import com.framework.core.constant.Globals;
import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.common.mappings.SysUrlMappings;
import com.framework.web.sys.domain.Role;
import com.framework.web.sys.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 角色信息管理——控制层
 *
 * @author fusf
 */
@Controller
public class RoleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Spring注入资源信息模块service
     */
    @Autowired
    private RoleService roleService;


    /**
     * 获取角色信息列表
     *
     * @param dgm
     * @param role
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_ROLE_LIST, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(DataGridModel dgm, Role role, HttpServletRequest request) throws Exception {
        PageRequest pr = DataGridModel.getPageRequest(dgm);
        return PageUtil.getPageMap(roleService.getPageList(role, pr));
    }

    @RequestMapping(value = SysUrlMappings.SYS_ROLE_TOSAVEANDUPDATE, method = RequestMethod.GET)
    public ModelAndView toSaveAndUpdate(@PathVariable("id") String id)
            throws Exception {
        // 进入基础数据类型添加或修改页面
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_ROLE_TOSAVEUPDATE);
        Role role = new Role();
        if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
            role = roleService.findById(id);
        } else {
            role.setStatus(Globals.STATUS_YES);
        }
        modelAndView.addObject("role", role);
        return modelAndView;
    }

    /**
     * 保存
     *
     * @param request
     * @param response
     * @param role
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_ROLE_SAVEANDUPDATE, method = RequestMethod.POST)
    @ResponseBody
    @SystemControllerLog(description = "保存角色信息")
    public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, Role role)
            throws Exception {
        AjaxJson j = new AjaxJson();
        boolean bValidate = this.validateRole(role);
        if (bValidate) {
            j.setSuccess(false);
            j.setMsg("角色名称重复");
        } else {
            if (StringUtils.isNotEmpty(role.getId())) {//更新
                Role r = roleService.findById(role.getId());
                MyBeanUtils.copyBeanNotNull2Bean(role, r);
                role = r;
            }
            roleService.save(role);//保存资源信息
            j.setMsg("角色信息保存成功");
        }
        return j;
    }


    private boolean validateRole(Role role) {
        boolean bValidate = false;
        //查询名字是否重复
      /*  List<Role> RoleList = RoleService.findByCategoryCodeAndOpenAccountId(Role.getCategoryCode(), Role.getOpenAccountId());
        if (RoleList != null && RoleList.size() > 0) {
            if (RoleList.size() > 1 || Role.getId() == null || Role.getId() == 0) { //新增状态
                bValidate = true;
            } else {
                if (Role.getId() != RoleList.get(0).getId()) {
                    bValidate = true;
                }
            }
        }*/
        return bValidate;
    }

    @RequestMapping(value = SysUrlMappings.SYS_ROLE_DELETE, method = RequestMethod.POST)
    @ResponseBody
    @SystemControllerLog(description = "删除角色信息")
    public AjaxJson RoleDelete(String id)
            throws Exception {
        AjaxJson j = new AjaxJson();
        roleService.delete(id);
        j.setMsg("删除成功");
        return j;
    }

    @RequestMapping(value = SysUrlMappings.SYS_ROLE_TOALLOCATERES, method = RequestMethod.GET)
    public ModelAndView toAllocateRes(@PathVariable("id") String id)
            throws Exception {
        // 进入基础数据类型添加或修改页面
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_ROLE_TOALLOCATERES);
        Role role = new Role();
        if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
            role = roleService.findById(id);
        }
        modelAndView.addObject("role", role);
        return modelAndView;
    }

}